﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Bill.cs" company="Concept Architecture">
//   (c) 2010 Muhammad Umer Farooq
// </copyright>
// <summary>
//   Defines the <seealso cref="Bill" /> type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Hisaab.Client.Model
{
    using System;
    using System.ComponentModel;

    /// <summary>
    ///  Defines the <seealso cref="Bill"/> type.
    /// </summary>
    public class Bill : ModelBase, IDataErrorInfo
    {
        #region Data Member

        /// <summary>
        /// Member variable _id
        /// </summary>
        private int _id;

        /// <summary>
        /// Member variable _dueDate
        /// </summary>
        private DateTime? _dueDate;

        /// <summary>
        /// Member variable _name
        /// </summary>
        private string _name;

        /// <summary>
        /// Backing f0ield for Amount
        /// </summary>
        private decimal _amount;

        /// <summary>
        /// Member variable _recurring
        /// </summary>
        private bool _recurring;

        /// <summary>
        /// Member variable _recurringRequency
        /// </summary>
        private RecurringFrequencyTypes _recurringFequency;

        /// <summary>
        /// Member variable _paid
        /// </summary>
        private bool _paid;

        /// <summary>
        /// Member variable _expiryDate
        /// </summary>
        private DateTime? _expiryDate;

        /// <summary>
        /// Member variable _error
        /// </summary>
        private string _error;

        #endregion Data Member

        #region Primitive Properties

        /// <summary>
        /// Gets or sets the id.
        /// </summary>
        /// <value>The id.</value>
        public int Id
        {
            get
            {
                return _id;
            }

            set
            {
                RaisePropertyChangingEvent("Id");
                _id = value;
                RaisePropertyChangedEvent("Id");
            }
        }

        /// <summary>
        /// Gets or sets the name.
        /// </summary>
        /// <value>The name.</value>
        public string Name
        {
            get
            {
                return _name;
            }

            set
            {
                RaisePropertyChangingEvent("Name");
                _name = value;
                RaisePropertyChangedEvent("Name");
            }
        }

        /// <summary>
        /// Gets or sets the amount.
        /// </summary>
        /// <value>
        /// The amount.
        /// </value>
        public decimal Amount
        {
            get
            {
                return _amount;
            }

            set
            {
                if (_amount != value)
                {
                    RaisePropertyChangingEvent("Amount");
                    _amount = value;
                    RaisePropertyChangedEvent("Amount");
                }
            }
        }

        /// <summary>
        /// Gets or sets the due date.
        /// </summary>
        /// <value>The due date.</value>
        public DateTime? DueDate
        {
            get
            {
                return _dueDate;
            }

            set
            {
                RaisePropertyChangingEvent("DueDate");
                _dueDate = value;
                RaisePropertyChangedEvent("DueDate");
            }
        }

        /// <summary>
        /// Gets or sets a value indicating whether this <see cref="Bill"/> is recurring.
        /// </summary>
        /// <value>true if recurring; otherwise, false.</value>
        public bool Recurring
        {
            get
            {
                return _recurring;
            }

            set
            {
                RaisePropertyChangingEvent("Recurring");
                _recurring = value;
                RaisePropertyChangedEvent("Recurring");
            }
        }

        /// <summary>
        /// Gets or sets a value indicating whether this <see cref="Bill"/> is paid.
        /// </summary>
        /// <value>true if paid; otherwise, false.</value>
        public bool Paid
        {
            get
            {
                return _paid;
            }

            set
            {
                RaisePropertyChangingEvent("Paid");
                _paid = value;
                RaisePropertyChangedEvent("Paid");
            }
        }

        /// <summary>
        /// Gets or sets the recurring fequency.
        /// </summary>
        /// <value>The recurring fequency.</value>
        public RecurringFrequencyTypes RecurringFequency
        {
            get
            {
                return _recurringFequency;
            }

            set
            {
                RaisePropertyChangingEvent("RecurringFequency");
                _recurringFequency = value;
                RaisePropertyChangedEvent("RecurringFequency");
            }
        }

        /// <summary>
        /// Gets or sets the expiry date.
        /// </summary>
        /// <value>The expiry date.</value>
        public DateTime? ExpiryDate
        {
            get
            {
                return _expiryDate;
            }

            set
            {
                RaisePropertyChangingEvent("ExpiryDate");
                _expiryDate = value;
                RaisePropertyChangedEvent("ExpiryDate");
            }
        }

        #endregion Primitive Properties

        #region Implementation of IDataErrorInfo

        /// <summary>
        /// Gets an error message indicating what is wrong with this object.
        /// </summary>
        /// <returns>
        /// An error message indicating what is wrong with this object. The default is an empty string ("").
        /// </returns>
        public string Error
        {
            get
            {
                return _error;
            }
        }

        /// <summary>
        /// Gets the error message for the property with the given name.
        /// </summary>
        /// <returns>
        /// The error message for the property. The default is an empty string ("").
        /// </returns>
        /// <param name="columnName">The name of the property whose error message to get. </param>
        public string this[string columnName]
        {
            get
            {
                string error = null;
                switch (columnName)
                {
                    case "Name":
                        if (string.IsNullOrWhiteSpace(Name))
                        {
                            error = "Name is requried.";
                        }

                        break;
                    case "Amount":
                        if (Amount <= 0)
                        {
                            error = "Amount is required.";
                        }

                        break;
                    case "DueDate":
                        if (DueDate <= new DateTime(2012, 01, 01))
                        {
                            error = "Valid due date is required.";
                        }

                        break;
                    case "ExpiryDate":
                        if (ExpiryDate.HasValue && ExpiryDate > DueDate)
                        {
                            error = "Expiry date should be later than due date.";
                        }

                        break;
                }

                _error = error;
                return error;
            }
        }

        #endregion Implementation of IDataErrorInfo
    }
}